package com.gds.wiki.cloud.orderCenter.server.oauth2.config;

import com.gds.wiki.cloud.framework.common.exception.BizExceptionEnum;
import com.gds.wiki.cloud.oauth2.common.model.SecurityMember;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

public class CustomTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        final Map<String, Object> additionalInfo = new HashMap<>();

        additionalInfo.put("code",BizExceptionEnum.SUCCESSFUL_OPERATION.getCode());
        SecurityMember securityMember= (SecurityMember) authentication.getPrincipal();
        additionalInfo.put("access",securityMember.getResourceList());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);

        return accessToken;
    }

}